<template>
    <div class="app-container">
        <el-card shadow="never">
            <!-- 条件查询 -->
            <el-form v-show="showSearch" ref="queryForm" :model="queryParams" label-width="120px" @keyup.enter.native="submitSearch" @submit.native.prevent>
                <el-row>
                    <el-col :md="6" :sm="12">
                        <el-form-item label="订单号:" prop="order_id.value">
                            <el-input type="textarea" :rows="4" v-model="queryParams.order_id.value" placeholder="请输入订单号" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :md="6" :sm="12">
                        <el-form-item label="SKU:" prop="sku.value">
                            <el-input v-model="queryParams.sku.value" placeholder="请输入SKU" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :md="6" :sm="12">
                        <el-form-item label="店铺:" prop="site_id.value">
                            <SelectPage v-model="queryParams.site_id.value" :url="'/admin/site/selectPage'" show_field="name" placeholder="请选择店铺" query_field="name" :multiple="false" />
                        </el-form-item>
                    </el-col>
                    <!-- <el-col :md="6" :sm="12">
                        <el-form-item label="创建时间:" prop="paid_time.value">
                            <el-date-picker
                                v-model="queryParams.paid_time.value"
                                type="datetimerange"
                                range-separator="至"
                                start-placeholder="开始"
                                end-placeholder="结束"
                                align="right"
                                value-format="yyyy-MM-dd HH:mm:ss"
                            >
                            </el-date-picker>
                        </el-form-item>
                    </el-col> -->

                    <el-col :md="6" :sm="12">
                        <el-form-item class="ele-text-right" label-width="50px">
                            <el-button type="primary" size="mini" @click="submitSearch" icon="el-icon-search" class="ele-btn-icon">搜索 </el-button>
                            <el-button @click="resetQuery" size="mini" icon="el-icon-refresh">重置</el-button>

                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>

            <el-row :gutter="10" class="mb8">
                <el-col :span="1.5">
                    <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleModification" v-hasPermi="api.shipping_documents">批量创建面单</el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button type="warning" plain size="mini" @click="table_log = true">批次列表</el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button type="info" plain icon="el-icon-download" size="mini" v-loading="exportLoading" @click="handleExport" v-hasPermi="api.export">导出</el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button type="danger" plain icon="el-icon-plus" size="mini" @click="handleprocessed()" v-hasPermi="api.processed">标记成已处理</el-button>
                </el-col>
                <right-toolbar :showSearch.sync="showSearch" @queryTable="initIndex" :columns="columns" printId="#quickadmin"></right-toolbar>
            </el-row>

            <el-drawer
                title="批次列表"
                :visible.sync="table_log"
                direction="rtl"
                size="50%">
                <el-table 
                :data="table_shipping_documents"
                v-loading="loading_log"
                >
                    <el-table-column property="add_time" label="时间" width=""></el-table-column>
                    <el-table-column property="num" label="数量" width=""></el-table-column>
                    <!-- <el-table-column property="status" label="状态"></el-table-column> -->
                    <el-table-column label="操作" width="">
                        <template slot-scope="scope">
                            <el-button class="btn-mar" size="mini" type="primary" @click="handleEdit(scope.row)" v-hasPermi="api.edit">订单明细</el-button>
                            <el-button type="info" plain size="mini" @click="download_info(scope.row)">导出</el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </el-drawer>

            <!-- 列表 -->
            <el-tabs v-model="activeTab" @tab-click="handleStatus" type="border-card">
                <el-tab-pane v-for="(item, index) in status" :key="index" :label="item.label" :name="item.value.toString()">
            <qa-table
                :source="tableData"
                :columns="columns"
                :selection="true"
                :loading="loading"
                @sortChange="sortChange"
                @statusChange="statusChange"
                @selectionChange="selectionChange"
                id="quickadmin"
            >
                <template v-slot:action>
                    <el-table-column label="操作" width="">
                        <template slot-scope="scope">
                            <el-button class="btn-mar" size="mini" type="info" @click="handleDetail(scope.row)" v-hasPermi="api.find">日志</el-button>
                        </template>
                    </el-table-column>
                </template>
            </qa-table>
            </el-tab-pane>
            </el-tabs>

            <el-dialog :title="title1" :visible.sync="dialogTableVisible">
                <el-table :data="msgdata">
                    <el-table-column property="msg" label="日志" width=""></el-table-column>
                </el-table>
            </el-dialog>

            <!-- 分页 -->
            <pagination :total="total" :page.sync="pageInfo.page" :limit.sync="pageInfo.limit" @pagination="initIndex" />
        </el-card>

        <!-- 编辑弹窗 -->
       <el-dialog :title="!disabled ? '编辑' : '详情'" :visible="editOpen"  :before-close="beforeClose" destroy-on-close>
            <EditForm ref="editForm" :findFormData="findFormData"  :disabled="disabled" @submitForm="submitForm" :editFormParams="editFormParams" @editFormCancel="beforeClose" />
        </el-dialog>

    </div>
</template>

<script>
import { curdMixin } from '@/mixins/curdMixin'
import EditForm from './EditFormCom'
export default {
    name: 'Curd',
    mixins: [curdMixin],
    components: { EditForm },
    
    data() {
        return {
            loading_log: false,
            table_log: false,
            table_shipping_documents: [],

            fileList: [],
            overseas_warehouse: '3',
            golden_ant: '1003',
            dialogTableVisible: false,
            title1: '请等待',
            // table结构
            columns: [
                {
                    visible: true,
                    label: '店铺',
                    prop: 'site_name',
                },
                // {
                //     visible: true,
                //     label: '订单状态',
                //     prop: 'status',
                // },
                {
                    visible: true,
                    label: '订单号',
                    prop: 'order_id',
                },
                {
                    visible: true,
                    label: 'SKU详情',
                    prop: 'skus_info',
                },
                {
                    visible: true,
                    label: '创建时间(美国)',
                    sortable: 'custom',
                    prop: 'createtime',
                },
            ],
            // 接口地址
            api: {
                index: `/admin/orderdocuments/index`,
                find: `/admin/orderdocuments/find`,
                add: `/admin/orderdocuments/add`,
                edit: `/admin/orderdocuments/edit`,
                delete: `/admin/orderdocuments/delete`,
                export: `/admin/orderdocuments/export`,
                shipping_documents: `/admin/orderdocuments/shipping_documents`,
                processed: `/admin/orderdocuments/processed`,
                status: `/admin/orderdocuments/status`
            },
            editFormParams: {
            },
            pageInfo: {
                sort: 'createtime',
            },
            primaryKey: 'id',
            // 搜索表单是否展开
            searchExpand: false,
              //是否展示搜索
            showSearch: true,
            // 查询参数
            queryParams: {
                order_id: { value:undefined, op: '%like%'},
                sku: { value:undefined, op: '%like%'},
                tracking_number: { value:undefined, op: '%like%'},
                name: { value:undefined, op: '%like%'},
                fulfillment_type: { value:undefined, op: '%like%'},
                paid_time: { value:[], op: 'range'},
                status: { value:[], op: '='},
                site_id: { value:[], op: '='},
                cancel_reason: { value:[], op: '='},
                order_status: { value:[], op: '='},
                shipping_type: { value:[], op: '='},
            },
            status: [
                { label: '等待创建面单', value: '' },
                { label: '正在创建中', value: 'create_waybill_currently' },
                { label: '创建面单成功', value: 'create_waybill_successfully' },
                { label: '创建面单失败', value: 'create_waybill_failed' },
                { label: '创建pdf失败', value: 'create_waybill_failed_pdf' },
                { label: '已处理', value: 'waybill_processed' },
            ],
            activeTab: '',
            msgdata:[],

        }
    },
    created () {
        this.initIndex()
    },
    watch: {
        
    },
    methods: {
        handleprocessed(row) {
            if(this.ids == ''){
                return this.msgError('请勾选再操作')
            }
            this.$confirm(`是否确认修改为已处理?`, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            })
                .then(() => {
                    this.processeds()
                })
                .catch(() => {})
        },
        async processeds() {
            await this.request.get(this.api.processed, { params: { id:this.ids } })
            this.initIndex()
            this.msgSuccess('操作成功')
        },
        // 案列：当前组件格式化方法
        testFormatter(prop) {
            return prop
        },

        submitUpload() {
            this.loading = true
            this.$refs.upload.submit();
        },
        handleRemove(file, fileList) {
            console.log(file, fileList);
        },
        handlePreview(file) {
            console.log(file);
        },
          //订单状态切换
        handleStatus(tab) {
            this.queryParams.status.value = tab.name
            this.submitSearch()
        },

        handleModification(row) {
            if(this.ids == ''){
                return this.msgError('请勾选再操作')
            }
            this.$confirm(`是否确认批量创建面单，过程大概需要一分钟?`, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            })
                .then(() => {
                    this.loading = true
                    this.loading_log = true
                    this.shipping_documents()
                })
                .catch(() => {})
        },

        async shipping_documents() {
            let response = await this.request.get(this.api.shipping_documents, { params: { id:this.ids} })
            this.initIndex()
            this.msgSuccess('操作成功，请查看批次列表')
            this.table_log = true
            this.loading = false
            this.loading_log = false
        },
        
        download_info(row) {
            console.log('download_info',row);
            this.msgError('正在生成面单，请等待')
        },

        // 编辑
        handleEdit(row) {
            this.handleFind(row.batch)
        },
        // 编辑回显
        async handleFind(batch) {
            let response = await this.request.get(this.api.edit, { params: 
                {
                    batch:batch,
                } })
            this.findFormData = this.formatFindData(response.data)
            this.editOpen = true
        },

        // 详情
        handleDetail (row) {
            if(this.queryParams.status.value == "create_waybill_successfully" || this.queryParams.status.value == "create_waybill_failed" || this.queryParams.status.value == "create_waybill_failed_pdf"){
                this.handleFindDetail(row.order_id)
                this.disabled = true
            }else{
                this.msgError('暂无日志，请先创建面单')
            }
        },

        // 详情回显
        async handleFindDetail (order_id) {
            let response = await this.request.get(this.api.find, { params: 
            {
                order_id:order_id,
            } })
            this.findFormData = this.formatFindData(response.data)
            this.editOpen = true
        },
    }
}
</script>
<style lang="scss">
.btn-mar {
    margin: 5px !important;
}
</style>
